package com.song.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.song.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author Klys
 * @Date 2022/4/4 23:10
 * @Version 1.0
 */
@Controller
@Slf4j
public class TestJson {
    @GetMapping("/testJson")
    @ResponseBody
    //    省略@RequestParam注解
    public String testJson(User user, String username, String password){
//        后端接收参数灵活：
//                  ①可以是封装对象 (User)
//                  ②可以是单个参数(username,password)
//                  ③可以封装对象与单个参数混用（User，username或password）
        System.out.println(user.getUsername());//hahah
        System.out.println(user.getPassword());//123456
        System.out.println(username);//hahah
        System.out.println(password);//123456
        System.out.println(user);
        log.debug("测试完成！");
        log.info("测试完成！");
        return "aaaa";
    }

    @GetMapping("/testJson2")
    @ResponseBody
//    加@RequestParam注解
    public String testJson2(@RequestParam String username, @RequestParam String password){
        System.out.println(username);//hahah
        System.out.println(password);//123456
        return "aaaa";
    }

    @PostMapping("/testJson3")
    @ResponseBody
//    ① 后端接收前端Json字符串，只能封装在User对象中，不能单独设置参数。
    public String testJson3(@RequestBody User user){
        System.out.println(user.getUsername());//hahah
        System.out.println(user.getPassword());//123456
        System.out.println(user);
        return "aaaa";
    }

    @RequestMapping(value = "/testJson4",method = {RequestMethod.POST})
    @ResponseBody
//    ② 后端接收前端Json字符串，封装到Map中
    public String testJson4(@RequestBody Map map){
        System.out.println(map.get("username"));//hahah
        System.out.println(map.get("password"));//123456
        map.put("Klys","123456");
        map.put("liusen","123456");
        System.out.println(map.get("Klys"));
        System.out.println(map);
        return "aaaa";
    }

    @RequestMapping(value = "/testJson5",method = {RequestMethod.POST})
    @ResponseBody
//    ③ 后端接收前端Json字符串，用String接收
    public String testJson5(@RequestBody String user) {
        System.out.println(); // {"username":"hahah","password":"123456"}
        System.out.println();//hahah
        System.out.println();//123456
        return "aaaa";
    }


}

